var ctx;
var pwdReg = /^(?![^a-zA-Z]+$)(?!\D+$).{8,30}$/; //至少包含字母数字的验证

function setCtx(ctx) {
	this.ctx = ctx;
}

var userPhone = $("#userPhone").val();
if( userPhone == 1){
var phoneBind = $("#phoneBind").val();
$("#phoneBind").val(phoneBind.substr(0,3) + "****" + phoneBind.substr(7,phoneBind.length+1));
}

var sixCode;
var phoneCode;
var timer;
//每隔60s发送一次验证码
	$("#sendcode").on("click",function(){
	    if( userPhone == 1){
			$.ajax({
				type : "POST",
				url: ctx + "/personalInfo/sendOldPhone",
				cache : false,
				dataType : 'json',
				success : function(result) {
					if (result.success) {
						layer.msg("验证码发送成功",{
							icon: 1,
							time:2000 //2秒关闭（如果不配置，默认是3秒）
						});
						$("#sixCode").val(result.obj);
						sixCode = $("#sixCode").val();
						resendTime();	
					} else {
						layer.msg("验证码发送失败",{
							icon: 2
						});
					}
				}
			});
			}
			
			if( userPhone == 2){
				if(checkPhone()){
					var phoneBind = $("#phoneBind").val();
					var userId = $("#userId").val();
					$.ajax({
						type : "POST",
						url: ctx + "/personalInfo/sendNewPhone",
						data:{
							phone:phoneBind, 
							id:userId
						},
						cache : false,
						dataType : 'json',
						success : function(result) {
							if (result.success) {
								layer.msg("验证码发送成功",{
									icon: 1,
									time: 2000 //2秒关闭（如果不配置，默认是3秒）
								});
								$("#sixCode").val(result.obj);
								sixCode = $("#sixCode").val();
								timer=undefined;
								resendTime();
							} else {
								layer.msg("验证码发送失败",{
									icon: 2
								});
							}
						}
					});
				}
					
				}
	});
		

function resendTime(){
	 var phone=$("#phoneBind").val();
	 
	    if(phone!="" && timer == null){
	        curTime=$("#resendTime").val();
	        $("#sendcode").attr("disabled",true);
	        $("#sendcode").css("background","grey");
	        $("#warning").text(curTime+"秒后可重新发送");
	        timer=setInterval(handleTimer, 1000);
	    }
}
function handleTimer(){
    if(curTime==0){
        clearInterval(timer);
        timer=null;
        $("#sendcode").removeAttr("disabled");
        $("#sendcode").css("background","#ffaa00");
        $("#warning").text("重新发送验证码");
    }
    else{
    	
        curTime--;
        $("#warning").text(curTime+"秒后可重新发送");
       
    }
}

//验证验证码
function checkCode(){
	var code = $("#phoneCode").val();
	//解决ie9以下placeholder与value重复的问题
	if(code == $("#phoneCode").attr("placeholder")){
		code = "";
	}

	if(code == null || code == ""){
		layer.tips('请输入验证码！', '#phoneCode', {
		    tips: 3,
		    time:1000
		});
		return false;
	} else if( code != sixCode) {
		layer.tips('验证码输入错误，请重新输入！', '#phoneCode', {
		    tips: 3,
		    time:1000
		});
		return false;
	} else {
		return true;
	}
}

//验证手机号
var phoneReg = /^(13[0-9]|15[0-9]|18[0-9]|14[578])[0-9]{8}$/;
function checkPhone(){
	var phoneBind = $("#phoneBind").val();
	//解决ie9以下placeholder与value重复的问题
	if(phoneBind == $("#phoneBind").attr("placeholder")){
		phoneBind = "";
	}

	if(phoneBind == null || phoneBind == ""){
		layer.tips('手机号不能为空！', '#phoneBind', {
		    tips: 3,
		    time:1000
		});
		return false;
	} else if(!phoneReg.test(phoneBind)){
		layer.tips('电话号码格式错误，请填写正确格式的电话号码!', '#phoneBind', {
		    tips: 3,
			time:1000
		});
		return false;
	} else {
		return true;
	}
}

function nextStep(){
	if(userPhone == 1){
		if(checkCode()){
			if(timer != null){
				clearInterval(timer);
			}
			loadContent("/personalInfo/bindPhone");
		}
	}
	if(userPhone == 2){
		if(checkPhone() && checkCode()){
			if(timer != null){
				clearInterval(timer);
			}
			loadContent("/personalInfo/bindComplete");
		}
			
	}
}

function Back(){
	if(timer != null){
		clearInterval(timer);
	}
	loadContent("/personalInfo/userCheck");
}
